package com.example.test_1;

import android.os.Bundle;
import android.view.MenuItem;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.example.test_1.fragment.AddressBookFragment;
import com.example.test_1.fragment.DiscoveryFragment;
import com.example.test_1.fragment.HomeFragment;
import com.example.test_1.fragment.MeFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.navigation.NavigationBarView;

import java.util.ArrayList;
import java.util.List;

public class DemoWeiXinButtom extends AppCompatActivity {

    List<Fragment> list;

     BottomNavigationView bottomNavigation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_demo_wei_xin_buttom);

        bottomNavigation = findViewById(R.id.bottomNavigation);

        list = new ArrayList<>();
        list.add(new HomeFragment());
        list.add(new AddressBookFragment());
        list.add(new DiscoveryFragment());
        list.add(new MeFragment());

        showFragment(list.get(0));

        //底部导航栏的切换事件
        bottomNavigation.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                if (item.getItemId()==R.id.menu_message){
                    showFragment(list.get(0));
                }
                else if (item.getItemId()==R.id.menu_address_book){
                    showFragment(list.get(1));

                }
                else if (item.getItemId()==R.id.menu_discovery){
                    showFragment(list.get(2));

                }
               else if (item.getItemId()==R.id.menu_me){
                    showFragment(list.get(3));
                }
                return true;
            }
        });
    }

    private void showFragment(Fragment fragment){
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.container,fragment);
        ft.commit();
    }
}